// pages/zhufu/zhufu.js
const app = getApp()
var util = require('../../utils/util.js');

Page({

  /**
   * 页面的初始数据
   */
  data: {
    backgroundImg:1,
    time:'',

    relation:0,
    festival:0,
    name:'',

    userHeader:'',
    userName:'',

    content:'',
    share: false,
    tishi:null
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    this.setData({
      tishi: app.globalData.isFirstEnterZhufu
    })
    if (options.content) {
      console.log(options);
      
      this.setData({
        share:true,
        tishi:false,
        userName: options.nickName,
        content: options.content
      })
    }
    else if (options.Recontent){
      this.setData({
        name: options.name,
        userName: options.userName,
        content: options.Recontent
      })
    }
    else{
      wx.request({
        url: app.globalData.url + 'get_random_wish/r/' + options.relation + '/f/' + options.festival,
        success: function (res) {
          that.setData({
            content: res.data.data,
            userName: options.nickName
          })
        }
      })
    }

    // 调用函数时，传入new Date()参数，返回值是日期和时间
    var time = util.YMD(new Date());      

    this.setData({
      time: time,

      relation: options.relation,
      festival: options.festival,
      name: options.name,

      userHeader: options.avatarUrl,
      
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this
    var data = that.data

    console.log(data);
    
    return {
      title: '许久不见，请收下我的祝福',
      path: '/pages/zhufu/result?relation=' + data.relation + '&festival=' + data.festival + '&name=' + data.name + '&avatarUrl=' + data.userHeader + '&nickName=' + data.userName + '&content=' + data.content,
    }
  },

  // 填写内容
  toCreate:function(){
    var name = this.data.name
    var userName = this.data.userName
    var content = this.data.content
    var relation = this.data.relation
    var festival = this.data.festival
    var userHeader = this.data.userHeader
    wx.navigateTo({
      url: 'write?name=' + name + '&userName=' + userName + '&content=' + content + '&relation=' + relation + '&festival=' + festival + '&avatarUrl=' + userHeader,
    })
  },

  // 换一个
  changeBg:function(e){
    var that = this
    var r = that.data.relation
    var f = that.data.festival
    wx.request({
      url: app.globalData.url + 'get_random_wish/r/' + r + '/f/' + f,
      success:function(res){
        that.setData({
          content: res.data.data
        })
      }
    })
  },

  //
  toIndex:function(){
    wx.navigateTo({
      url: '/pages/zhufu/zhufu'
    })
  },

  // 关闭提示
  know:function(){
    app.globalData.isFirstEnterZhufu = false
    this.setData({
      tishi: app.globalData.isFirstEnterZhufu
    })
  }
})